// pages/organization/add/index.js
import {
  postRidingEventsApi
} from '../../../https/ridingEvents'
import {
  BASE_URL
} from '../../../constant/index'
import {
  uploadFileAsync
} from '../../../utlis/utils'
Page({
  data: {
    BASE_URL,
    autosize: {
      maxHeight: 200,
      minHeight: 150
    },
    fileList: [],
    detail: {
      cover: '',
      location: '南宁市西乡塘区广西民族大学',
      cost: 0,
      maxParticipants: 10,
      eventTime: '',
    },
    date: '',
    time: '',
    description: '',
    title: ''
  },

  /**
   * 生命周期函数--监听页面加载
   */
  onLoad(options) {

  },
  // 切换人数
  async bindChange(e) {
    console.log(e)
    const type = e.currentTarget.dataset.type
    const maxParticipants = this.data.detail.maxParticipants
    console.log(type)
    if (type == 'add') {
      this.setData({
        'detail.maxParticipants': maxParticipants + 1
      })
    } else {
      if (maxParticipants <= 1) return
      this.setData({
        'detail.maxParticipants': maxParticipants - 1
      })
    }
  },
  // 选择如期
  bindPickerChange(e) {
    console.log(e)
    const value = e.detail.value
    const type = e.currentTarget.dataset.type
    this.setData({
      [type]: value
    })
  },
  // 获取地址
  async bindAddress() {
    const {
      type,
      city,
      address
    } = await wx.choosePoi()
    if (type === 1) {
      this.setData({
        'detail.location': city
      })
    } else
    if (type === 2) {
      this.setData({
        'detail.location': address
      })
    } else {
      this.setData({
        'detail.location': ''
      })
    }
  },
  // 上传图片
  async bindUpload() {
    const {
      tempFiles
    } = await wx.chooseMedia({
      count: 1,
      mediaType: ['image']
    })
    wx.showLoading({
      title: '上传中..',
    })
    const uploadList = tempFiles.map(item => {
      return new Promise(async (resolv) => {
        const url = await uploadFileAsync(item.tempFilePath)
        resolv(url)
      })
    })
    console.log(uploadList)

    const urlList = await Promise.all(uploadList)

    this.setData({
      'detail.cover': urlList[0]
    })

    wx.hideLoading()
  },
  // 上传按钮
  async bindcreat() {
    // 判断
    console.log(2)
    const {
      cost,
      cover,

      location,
      maxParticipants,

    } = this.data.detail
    const {
      description,
      title
    } = this.data
    const {
      date,
      time
    } = this.data
    console.log(this.data)
    console.log(!cover, !description, !location, !title);
    console.log(cover, description, location, title);
    if (!cover || !description || !location || !title) {
      wx.showToast({
        title: '请完善表单',
        icon: 'none'
      })
      return
    }
    if (!date || !time) {
      wx.showToast({
        title: '需要选择时间',
        icon: 'none'
      })
      return
    }
    wx.showLoading({
      title: '创建中..',
    })

    const eventTime = (new Date(date + ' ' + time)).toISOString()

    const postRes = await postRidingEventsApi({
      cover,
      title,
      location,
      cost,
      maxParticipants,
      eventTime,
      description
    })
    wx.hideLoading()
    console.log(postRes)
    if (postRes.code == 200) {
      wx.showToast({
        title: '创建成功',
        icon: 'success',
        mask: true
      })
      setTimeout(() => {
        wx.navigateBack()
      }, 1000);
    } else {
      wx.showToast({
        title: '创建失败',
        icon: 'error'
      })
    }
  }

})